<!--注册页面-->
<template>
    <div style="height: 100vh; display: flex; align-items: center; justify-content: center;background-color: #08acec">
        <div style="display: flex; background-color: white; width: 65%; border-radius: 5px; overflow: hidden">
            <div style="flex: 1">
                <img src="../assets/register.png" alt="" style="width: 100%;">
            </div>
            <div style="flex: 1; display: flex; align-items: center; justify-content: center">
                <el-form :model="user" style="width: 60%" :rules="rules" ref="registerRef">
                    <div style="font-size: 24px; font-weight: bold; text-align: center; margin-bottom: 10px">欢迎注册学科竞赛管理系统</div>
                    <div style="margin-top: 20px">
                        <el-form-item prop="userid">
                            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入10位以内账号"
                                      v-model="user.userid"></el-input>
                        </el-form-item>

                        <el-form-item prop="password">
                            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入8位以内密码"
                                      v-model="user.password"></el-input>
                        </el-form-item>

                        <el-form-item prop="confirmPass">
                            <div style="display: flex">
                                <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码"
                                          v-model="user.confirmPass"></el-input>
                            </div>
                        </el-form-item>

                        <el-form-item prop="phone">
                            <div style="display: flex">
                                <el-input prefix-icon="el-icon-phone-outline" size="medium" placeholder="请输入手机号"
                                          v-model="user.phone"></el-input>
                            </div>
                        </el-form-item>

                        <el-form-item prop="major">
                            <el-select v-model="user.major" placeholder="请选择学部">
                                <el-option
                                        v-for="item in options"
                                        :key="item"
                                        :label="item"
                                        :value="item">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item prop="role">
                            <el-radio-group v-model="user.role">
                                <el-radio label="学生"></el-radio>
                                <el-radio label="教师"></el-radio>
                                <!--                            <el-radio label="管理员"></el-radio>    管理员不能通过注册得到角色-->
                            </el-radio-group>
                        </el-form-item>
                    </div>

                    <el-form-item>
                        <el-button type="primary" style="width: 100%; border-radius: 5px;font-size: 16px" @click="register">注 册</el-button>
                    </el-form-item>
                    <div style="display: flex">
                        <div style="flex: 1">已经有账号了？请<span style="color: #0795f5; cursor: pointer" @click="$router.push('/login')">登录</span></div>
                    </div>
                </el-form>
            </div>

        </div>

    </div>
</template>

<script>
    import { cellphoneValidator } from '@/utils/userPhone';

    export default {
        name: "Register",

        data() {

            //验证码校验
            const validatePassword = (rule, confirmPass, callback) => {
                if (confirmPass === '') {
                    callback(new Error('请确认密码'));
                } else if (confirmPass !== this.user.password) {
                    callback(new Error('两次输入的密码不一致'));
                }else {
                    callback();
                }
            }

            return {
                user: {
                    confirmPass: '', //表单中用户输入的确认密码
                    userid: '',
                    password: ''
                },

                //学部
                options: [],

                rules:{//规则判断验证
                    userid:[
                        {required: true, message:'请输入账号', trigger:'blur'},
                    ],
                    password:[
                        {required: true, message:'请输入密码', trigger:'blur'},
                    ],
                    confirmPass:[
                        { validator: validatePassword, trigger:'blur'}
                    ],
                    phone:[
                        {required: true, message:'请输入手机号', trigger:'blur'},
                        { validator: cellphoneValidator, trigger: 'blur' }
                    ],
                    major:[
                        {required: true, message:'请选择学部', trigger:'blur'},
                    ],
                    role:[
                        {required: true, message:'请选择角色', trigger:'blur'},
                    ],
                }
            }
        },
        created() {
            this.load()
        },

        methods: {
           load(){
               //接收学部类型值
               this.$request.get('/faculty/selectAll').then(res => {
                   this.options = res.data
                   console.log(res)
                   console.log(this.options)
               })
           },
            register(){
                this.$refs['registerRef'].validate((valid) => {
                    if (valid){
                        //验证通过
                        this.$request.post('/register',this.user).then(res => {
                            if (res.code === '200'){
                                this.$router.push('/login')
                                this.$message.success('注册成功')
                            }else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                })

            }
        }
    }
</script>

<style scoped>

</style>